﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace HomeWork5
{
    public class OrderService
    {
        public
            List<Order> orders = new List<Order>();
        //添加订单
        public OrderService() { }
        public bool AddOrder(Order orderNew)
        {
            bool repeated = false;
            foreach (Order order in this.orders)
            {
                if (order.Equals(orderNew))
                    repeated = true;
            }
            if (!repeated)
            {
                this.orders.Add(orderNew);
                Console.WriteLine("添加成功！");
            }
            else { Console.WriteLine("已有重复订单！"); }
            return !repeated;
        }
        //删除订单
        public bool DeleteOrder(Order orderOld)
        {
            bool existence = false;
            Order orderRomoving=new Order();
            orders.ForEach(order =>
            {
                if (order.Equals(orderOld))
                {
                    orderRomoving =order;
                    existence = true;
                }
            });
            if (existence)
            { Console.WriteLine("删除成功！");
                orders.Remove(orderRomoving);
            }
            else
                throw new OrderException();
            return existence;
        }
        //修改订单
        public bool ModifyOrder(Order orderWrong, Order orderRight)
        {
            bool existence = false;
            Order orderRomving = new Order();
            orders.ForEach(order =>
            {
                if (order.Equals(orderWrong))
                {
                    orderRomving = orderWrong;
                    existence = true;
                }
            });
            if (existence)
            {
                orders.Remove(orderRomving);
                orderRight.OrderID = orderWrong.OrderID;
                orders.Add(orderRight);
                Console.WriteLine("修改成功！");
            }
            else
                throw new OrderException ();
            return existence;
        }
        //按订单号查询
        public List<Order> FindByOrderID(int orderID)
        {
            List<Order> rightOrder = new List<Order>();
            var q = orders
                .Where(o => o.OrderID == orderID)
                .OrderBy(o => o.TotalMoney);
            foreach (Order order in q) { Console.WriteLine(order); rightOrder.Add(order); }
            return rightOrder;
        }
        //按客户名查询
        public List<Order> FindByClientName(string clientName)
        {
            List<Order> rightOrder = new List<Order>();
            var q = orders
               .Where(o => o.Client.ClientName == clientName)
               .OrderBy(o => o.TotalMoney);
            foreach (Order order in q) { Console.WriteLine(order); rightOrder.Add(order); }
            return rightOrder;
        }
        //按商品名查询
        public List<Order> FindByProductName(string productName)
        {
            List<Order> rightOrder = new List<Order>();
            foreach (Order order1 in orders)
            {
                var q = order1.OrderItems
                    .Where(oi => oi.Product.ProductName == productName);
                if (q.Count() != 0) { Console.WriteLine(order1); rightOrder.Add(order1); }
            }
            return rightOrder;
        }
        //默认按订单号排序
        public void OrderSort()
        {
            orders.Sort();
            ShowAllOrder();
        }

        //按客户称排序
        public void OrderSortByClient()
        {
            orders.Sort((order1, order2) => order1.Client.CompareTo(order2.Client));
            ShowAllOrder();
        }
        //按订单总金额排序
        public void OrderSortByTotalMoney()
        {
            orders.Sort((order1, order2) => order1.TotalMoney.CompareTo(order2.TotalMoney));
            ShowAllOrder();
        }
        //展示所有订单
        public void ShowAllOrder()
        {
            orders.ForEach(order => Console.Write(order));
        }
        //将所有的订单转化为XML文件
        public void Export()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using(FileStream fs=new FileStream("Order.xml",FileMode.Create))
            {
                xmlSerializer.Serialize(fs, orders);
            }
            Console.WriteLine("订单转化为XML文件成功！");
        }
        //从XML文件中读取订单
        public void Import()
        {
            try
            {
                XmlSerializer xmlserializer = new XmlSerializer(typeof(List<Order>));
                using (FileStream fs = new FileStream("Order.xml", FileMode.Open))
                {
                    orders = (List<Order>)xmlserializer.Deserialize(fs);
                }
                Console.WriteLine("载入订单成功！");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("文件未找到！");
            }
        }
    }
}
